RegisterForm = {
	
	firstNameValidated 	: false,
	lastNameValidated 	: false,
	sexValidated		: false,
	languageValidated	: false,
	usernameValidated 	: false,
	password1Validated	: false,
	password2Validated	: false,
	email1Validated 	: false,
	email2Validated 	: false,
	captchaValidated	: false,
	domain : document.domain,
	
	init:function()
	{
		var firstName 		= document.getElementById("firstName");
		var lastName 		= document.getElementById("lastName");
		var sex				= document.getElementById("sex");
		var language		= document.getElementById("language");
		var username 		= document.getElementById("username");
		var password1		= document.getElementById("password1");
		var password2		= document.getElementById("password2");
		var email1 			= document.getElementById("email1");
		var email2 			= document.getElementById("email2");
		var captchaInput 	= document.getElementById("captcha-input");
		var captchaId 		= document.getElementById("captcha-id");
		var submit			= document.getElementById("submit");
		
		
		if (firstName !== null) 
		{
			firstName.onkeyup = RegisterForm.validateFirstName;
		}
		
		if (lastName !== null) 
		{
			lastName.onkeyup = RegisterForm.validateLastName;
		}
		
		if (sex !== null) 
		{
			sex.onchange = RegisterForm.validateSex;
		}
		
		if (language !== null)
		{
			language.onchange = RegisterForm.validateLanguage;
		}
		
		if (username !== null) 
		{
			username.onkeyup = RegisterForm.validateUsername;
		}
		
		if (password1 !== null) 
		{
			password1.onkeyup = RegisterForm.validatePassword1;
		}
		
		if (password2 !== null) 
		{
			password2.onkeyup = RegisterForm.validatePassword2;
		}

		if (email1 !== null) 
		{
			email1.onkeyup = RegisterForm.validateEmail1;
		}
		
		if (email2 !== null) 
		{
			email2.onkeyup = RegisterForm.validateEmail2;
		}
		
		if (captchaInput !== null && captchaId !== null) 
		{
			captchaInput.onkeyup = RegisterForm.validateCaptcha;
		}
		
		submit.disabled = true;
	},
	
	validateFirstName:function()
	{
		var inputField = document.getElementById("firstName");
		var value = inputField.value;
		var span;
		
		if (inputField.parentNode.lastChild == inputField)
		{
			span = document.createElement("span");
			span.setAttribute("class", "ajaxFormValidation");
			span.setAttribute("id", "ajaxFormValidationFirstName");
			inputField.parentNode.appendChild(span);
		}
		else
		{
			span = document.getElementById('ajaxFormValidationFirstName');
		}
		
		if(validator.validateFirstName(value))
		{	
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/success.png" alt="success" />';
			RegisterForm.firstNameValidated = true;
		}
		else
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/error.png" alt="error" />';
			RegisterForm.firstNameValidated = false;
		}
		
		RegisterForm.EnableSubmitButton();
	},
	
	validateLastName:function()
	{
		var inputField = document.getElementById("lastName");
		var value = inputField.value;
		var span;
		
		if (inputField.parentNode.lastChild == inputField)
		{
			span = document.createElement("span");
			span.setAttribute("class", "ajaxFormValidation");
			span.setAttribute("id", "ajaxFormValidationLastName");
			inputField.parentNode.appendChild(span);
		}
		else
		{
			span = document.getElementById('ajaxFormValidationLastName');
		}
		
		if(validator.validateLastName(value))
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/success.png" alt="success" />';
			RegisterForm.lastNameValidated = true;
		}
		else
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/error.png" alt="error" />';
			RegisterForm.lastNameValidated = false;
		}
		
		RegisterForm.EnableSubmitButton();
	},
	
	validateSex:function()
	{
		var inputField = document.getElementById("sex");
		var value = inputField.value;
		var span;
		
		if (inputField.parentNode.lastChild == inputField)
		{
			span = document.createElement("span");
			span.setAttribute("class", "ajaxFormValidation");
			span.setAttribute("id", "ajaxFormValidationSex");
			inputField.parentNode.appendChild(span);
		}
		else
		{
			span = document.getElementById('ajaxFormValidationSex');
		}
		
		if(value == 'male' || value == 'female')
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/success.png" alt="success" />';
			RegisterForm.sexValidated = true;
		}
		else
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/error.png" alt="error" />';
			RegisterForm.sexValidated = false;
		}
		
		RegisterForm.EnableSubmitButton();
	},
	
	validateLanguage:function()
	{
		var inputField = document.getElementById("language");
		var value = inputField.value;
		var span;

		if (inputField.parentNode.lastChild == inputField)
		{
			span = document.createElement("span");
			span.setAttribute("class", "ajaxFormValidation");
			span.setAttribute("id", "ajaxFormValidationLanguage");
			inputField.parentNode.appendChild(span);
		}
		else
		{
			span = document.getElementById('ajaxFormValidationLanguage');
		}
		
		if (value != '0')
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/success.png" alt="success" />';
			RegisterForm.languageValidated = true;
		}
		else
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/error.png" alt="error" />';
			RegisterForm.languageValidated = false;
		}
				
		RegisterForm.EnableSubmitButton();
	},
	
	validateUsername:function()
	{
		var inputField = document.getElementById("username");
		
		if (inputField.parentNode.lastChild == inputField)
		{
			var span = document.createElement("span");
			span.setAttribute("class", "ajaxFormValidation");
			span.setAttribute("id", "ajaxFormValidationUsername");
			inputField.parentNode.appendChild(span);
		}

		var value = inputField.value;		
		
		ajaxCon.readData("ajaxFormValidationUsername",'http://' + document.domain + '/index/ajaxregistervalidation?username='+value, RegisterForm.UserNameValidationRetrived);
		
		RegisterForm.EnableSubmitButton();
	},
	
	validatePassword1:function()
	{
		var inputField = document.getElementById("password1");
		var value = inputField.value;
		var span;
		
		if (inputField.parentNode.lastChild == inputField)
		{
			span = document.createElement("span");
			span.setAttribute("class", "ajaxFormValidation");
			span.setAttribute("id", "ajaxFormValidationPassword1");
			inputField.parentNode.appendChild(span);
		}
		else
		{
			span = document.getElementById('ajaxFormValidationPassword1');
		}
		
		if(validator.validatePassword(value))
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/success.png" alt="success" />';
			RegisterForm.password1Validated = true;
		}
		else
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/error.png" alt="error" />';
			RegisterForm.password1Validated = false;
		}
		
		if(document.getElementById('ajaxFormValidationPassword2') !== null)
		{
			RegisterForm.validatePassword2();
		}
		
		RegisterForm.EnableSubmitButton();
	},
	
	validatePassword2:function()
	{
		var inputField = document.getElementById("password2");
		var value1 = document.getElementById("password1").value;
		var value2 = inputField.value;
		var span;
		
		if (inputField.parentNode.lastChild == inputField)
		{
			span = document.createElement("span");
			span.setAttribute("class", "ajaxFormValidation");
			span.setAttribute("id", "ajaxFormValidationPassword2");
			inputField.parentNode.appendChild(span);
		}
		else
		{
			span = document.getElementById('ajaxFormValidationPassword2');
		}
		
		if(value1 === value2)
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/success.png" alt="success" />';
			RegisterForm.password2Validated = true;
		}
		else
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/error.png" alt="error" />';
			RegisterForm.password2Validated = false;
		}
				
		RegisterForm.EnableSubmitButton();
	},

	validateEmail1:function()
	{
		var inputField = document.getElementById("email1");
		
		if (inputField.parentNode.lastChild == inputField)
		{
			var span = document.createElement("span");
			span.setAttribute("class", "ajaxFormValidation");
			span.setAttribute("id", "ajaxFormValidationEmail");

			inputField.parentNode.appendChild(span);
		}
		var value = inputField.value;		
				
		ajaxCon.readData("ajaxFormValidationEmail",'http://' + document.domain + '/index/ajaxregistervalidation?email='+value, RegisterForm.EmailValidationRetrived);
		
		if(document.getElementById('ajaxFormValidationEmail2') !== null)
		{
			RegisterForm.validateEmail2();
		}
		
		RegisterForm.EnableSubmitButton();
	},
	
	validateEmail2:function()
	{
		var inputField = document.getElementById("email2");
		var value1 = document.getElementById("email1").value;
		var value2 = inputField.value;
		var span;
		
		if (inputField.parentNode.lastChild == inputField)
		{
			span = document.createElement("span");
			span.setAttribute("class", "ajaxFormValidation");
			span.setAttribute("id", "ajaxFormValidationEmail2");
			inputField.parentNode.appendChild(span);
		}
		else
		{
			span = document.getElementById('ajaxFormValidationEmail2');
		}
		
		if(value1 === value2)
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/success.png" alt="success" />';
			RegisterForm.email2Validated = true;
		}
		else
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/error.png" alt="error" />';
			RegisterForm.email2Validated = false;
		}
		
		
		RegisterForm.EnableSubmitButton();
	},
	
	validateCaptcha:function()
	{
		var inputField = document.getElementById("captcha-input");
		var value = inputField.value;
		var span;
		
		
		if (inputField.parentNode.lastChild == inputField)
		{
			span = document.createElement("span");
			span.setAttribute("class", "ajaxFormValidation");
			span.setAttribute("id", "ajaxFormValidationCaptcha");
			inputField.parentNode.appendChild(span);
		}
		else
		{
			span = document.getElementById('ajaxFormValidationCaptcha');
		}
		
		
		
		if(value1 === value2)
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/success.png" alt="success" />';
			RegisterForm.captchaValidated = true;
		}
		else
		{
			span.innerHTML = '<img src="http://' + document.domain + '/pics/form/error.png" alt="error" />';
			RegisterForm.captchaValidated = false;
		}
		
		
		
		RegisterForm.EnableSubmitButton();
	},
	
	UserNameValidationRetrived:function(responseText)
	{
		var container = document.getElementById("ajaxFormValidationUsername");
		container.innerHTML = responseText;
		var pic  = container.firstChild;
		var picSrc = pic.getAttribute('src');
		
		picSrc = picSrc.substring(picSrc.length - 11,picSrc.length - 4);
		
		if(picSrc == 'success')
		{
			RegisterForm.usernameValidated = true;
		}
		else
		{
			RegisterForm.usernameValidated = false;
		}
		
		RegisterForm.EnableSubmitButton();
	},
	
	EmailValidationRetrived:function(responseText)
	{
		var container = document.getElementById("ajaxFormValidationEmail");
		container.innerHTML = responseText;
		
		var pic  = container.firstChild;
		var picSrc = pic.getAttribute('src');
		
		picSrc = picSrc.substring(picSrc.length - 11,picSrc.length - 4);
		
		if(picSrc == 'success')
		{
			RegisterForm.email1Validated = true;
		}
		else
		{
			RegisterForm.email1Validated = false;
		}
		
		RegisterForm.EnableSubmitButton();
	},

	EnableSubmitButton:function()
	{
		var button = document.getElementById("submit");
		
		if(	RegisterForm.firstNameValidated	== true &&
			RegisterForm.lastNameValidated 	== true &&
			RegisterForm.sexValidated		== true &&
			RegisterForm.languageValidated	== true &&
			RegisterForm.usernameValidated 	== true &&
			RegisterForm.password1Validated	== true &&
			RegisterForm.password2Validated	== true &&
			RegisterForm.email1Validated 	== true &&
			RegisterForm.email2Validated 	== true)
			{
				button.disabled = false;
			}
			else
			{
				button.disabled = true;
			}
	}
}

window.onload = RegisterForm.init;


